<?php
namespace App\Admin\Controllers\Basics;

use App\Admin\Actions\Basics\ExchangeRate\ImportEr;
use App\Models\Basics\Currency;
use App\Models\Basics\ExchangeRate;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class ExchangeRateController extends AdminController{

    protected $title = '汇率';

    protected function grid()
    {
        $grid = new Grid(new ExchangeRate());
        $grid->model()->orderBy('id','DESC');
        $grid->column('id','ID')->style('width: 5%');
        $grid->column('currency_code','币种');
//        $grid->column('currency.name','名称');
        $grid->column('effective_at','有效期（月）');
        $grid->column('rate_odds', '汇率');

        if(Admin::user()->can('exchange-rate.edit')){
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
            });
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(new ImportEr());
            });
        }else{
            $grid->disableActions();
            $grid->disableCreateButton();
        }

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->equal('currency_code', '币种')->select(Currency::options());
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('effective_at', '有效期')->datetime(['format' => 'YYYY-MM-01']);
            });
        });

        return $grid;
    }

    public function form()
    {
        $form = new Form(new ExchangeRate());

        $form->hidden('id');

        $form->select('currency_code','币种')->options(Currency::options())
            ->required()
            ->help('若币种不存在请先添加')->setWidth(2);
        $form->date('effective_at', '有效期（月）')->format('YYYY-MM-01')->style('width','150px')->required();
        $form->decimal('rate_odds', '汇率')->options(['maxlength' =>8,'digits'=>4])->style('width','150px')->required();
        $form->saving(function (Form $form) {
            // 判断是否是新增操作
            if ($form->isCreating()) {
                $count = ExchangeRate::query()
                    ->where('currency_code',$form->currency_code)
                    ->where('effective_at',$form->effective_at)
                    ->count();
                if($count > 0){
                    admin_error('重复添加','该币种有效期内已存在');
                    return back()->withInput();
                }
            }else{
                $count = ExchangeRate::query()
                    ->where('id','<>',$form->model()->id)
                    ->where('currency_code',$form->currency_code)
                    ->where('effective_at',$form->effective_at)
                    ->count();
                if($count > 0){
                    admin_error('重复添加','该币种有效期内已存在');
                    return back()->withInput();
                }
            }
        });
        if($form->isCreating()){
            $form->disableCreatingCheck(false);
        }
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
